const http=require("http");
const express=require("express");
const routerIndex=require("./router/index");
const routerLogin=require("./router/login");
const routerRegister=require("./router/register");
const routerNingmeng=require("./router/ningmeng");
const routerQinshi=require("./router/qinshi");
const routerShitang=require("./router/shitang");
const routerStore=require("./router/store");


let app=express();
let server=http.createServer(app);
server.listen(3000);
console.log("Server is running")
//使用中间件
app.use(express.urlencoded({
  extended:false
}));

// 加载CORS模块
const cors = require('cors');

// 使用CORS中间件
app.use(cors({
  origin: ['http://localhost:8080', 'http://127.0.0.1:8080']
}));

app.use("/index",routerIndex);
app.use("/login",routerLogin);
app.use("/register",routerRegister);
app.use("/ningmeng",routerNingmeng);
app.use("/qinshi",routerQinshi);
app.use("/shitang",routerShitang);
app.use("/store",routerStore);

//使用错误处理中间件，拦截所有路由中抛出的错误
app.use((err,req,res,next)=>{
  console.log(err);
  //设置http协议响应的状态码
  res.status(500).send({code:500,msg:'服务器端错误'});
});
